package com.cloudfast.platform.org.controller;

import com.cloudfast.common.user.UserUtils;
import com.cloudfast.context.error.CloudException;
import com.cloudfast.context.error.ErrorCode;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cloudfast.common.page.Page;
import com.cloudfast.common.result.ApiResponse;
import com.cloudfast.controller.BaseController;
import com.cloudfast.platform.org.entity.Org;
import com.cloudfast.platform.org.service.OrgService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuyw
 * @描述:
 * @date 2020年4月30日
 */
@RestController
@RequestMapping(value = "org")
@Api(value = "机构管理", tags = "机构管理")
public class OrgController extends BaseController<Org> {

    @Autowired
    private OrgService orgService;

    @Override
    public ApiResponse findOne(Long id) {
        try {
            return ApiResponse.success(this.orgService.findOne(id));
        } catch (Exception e) {
            throw new CloudException(ErrorCode.FIND_ONE_ERROR);
        }
    }

    @Override
    public ApiResponse save(Org entity) {
        try {
            orgService.save(entity);
            return ApiResponse.success("操作成功");
        } catch (Exception e) {
            throw new CloudException(ErrorCode.SAVE_ERROR);
        }
    }

    @Override
    public ApiResponse findPage(Page<Org> page, Org entity) {
        try {
            return ApiResponse.success(this.orgService.findPage(page, entity));
        } catch (Exception e) {
            throw new CloudException(ErrorCode.FIND_PAGE_ERROR);

        }
    }

    @Override
    public ApiResponse delete(Long id) {
        try {
            orgService.delete(id);
            return ApiResponse.success(true);
        } catch (Exception e) {
            throw new CloudException(ErrorCode.DELETE_ERROR);

        }

    }

    /**
     * @param id
     * @param orgCode
     * @return
     * @描述:检测编号是否存在
     * @author liuyw
     * @date 2020年4月20日
     */
    @PostMapping(value = "checkCode")
    @ApiOperation("检测机构编码是否存在")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键", required = true),
            @ApiImplicitParam(name = "orgCode", value = "机构编码", required = true)})
    public ApiResponse checkCode(Long id, String orgCode) {
        try {
            return ApiResponse.success(this.orgService.checkCode(id, orgCode));
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new CloudException(ErrorCode.DELETE_ERROR);
        }
    }

    /**
     * 拉下树
     *
     * @return
     */
    @ApiOperation("异步加载树状架")
    @PostMapping(value = "treeSelect")
    public ApiResponse treeSelect(Long parentId) {
        try {
            List<Org> list = new ArrayList<>();

            if (parentId == -1) {
                Org org = orgService.findOne(UserUtils.getUser().getOrgId());
                list.add(org);
            } else {
                list = this.orgService.findSelectTree(parentId);
            }
            return ApiResponse.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CloudException(ErrorCode.SYSTEM_ERROR);
        }
    }
}
